<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/14
 * Time: 13:36
 *
 * @copyright: ©2024 永州 微聚商网络科技有限公司
 * @link: https://www.wEijuyunke.com
 */

namespace app\admin\controller;

use app\common\model\DaziJoin;
use app\common\model\Label;
use app\Request;

class  TabClass{
    public function tabList(Request $request){
        $param = $request->param();
        $where = [];
        if (isset($param['top_class']) && !empty($param['top_class'])){
            $where[] = ['pid','=',0];
        }
        if (isset($param['pid']) && !empty($param['pid'])){
            $where[] = ['pid','=',$param['pid']];
        }

        if (isset($param['name']) && !empty($param['name'])){
            $where[] = ['name','like','%'.$param['name'].'%'];
        }

        if (isset($param['type']) && !empty($param['type'])){
            $where[] = ['type','=',$param['type']];
        }

        if (isset($param['status']) && !empty($param['status'])){
            $where[] = ['status','=',$param['status']];
        }else{
            $where[] = ['status','<>',2];
        }
        $tabClassModel = new \app\common\model\TabClass();
        $tabList = $tabClassModel->where($where)->order('sort','asc')->select()->toArray();
        $count = $tabClassModel->where($where)->select()->count();
        return success('分类列表',['list' => $tabList,'count' => $count]);
    }
    public function  addTab(Request $request){
        $param = $request->param();
        $data = [
            'name' => $param['name'],
            'type' => $param['type'],
            'create_time' =>  time(),
            'pid' =>  $param['pid'],
            'sort' => $param['sort'],
        ];
        if (isset(  $param['icon'])){
            $data['icon'] = $param['icon'];
        }
        if (isset(  $param['label_icon'])){
            $data['label_icon'] = $param['label_icon'];
        }
        if (isset(  $param['type_css'])){
            $data['type_css'] = $param['type_css'];
        }
        $tabClassModel = new \app\common\model\TabClass();
        $tabClassModel->insert($data);
        return success('添加成功');
    }
    public function  editTab(Request $request){
        $param = $request->param();
        $data = [
            'name' => $param['name'],
            'type' => $param['type'],
            'pid' =>  $param['pid'],
            'icon' =>  $param['icon'],
            'sort' => $param['sort'],
        ];
        if (isset($param['label_icon'])){
            $data['label_icon'] = $param['label_icon'];
        }
        if (isset(  $param['type_css'])){
            $data['type_css'] = $param['type_css'];
        }
        $tabClassModel = new \app\common\model\TabClass();
        $tabInfo = $tabClassModel->where(['id' => $param['id']])->find();

        if ($param['pid']  > 0){
            if ($param['pid'] != $tabInfo['pid']){
                $daziModel  = new \app\common\model\Dazi();
                $daziInfo  = $daziModel->where(['pid_tab' => $tabInfo['pid']])->find();
                if ($daziInfo){
                     return error('已有搭子绑定此分类,请勿修改上级分类');
                }
                $serviceModel   = new \app\common\model\Service();
                $serviceInfo  = $serviceModel->where(['pid_tab' => $tabInfo['pid']])->find();
                if ($serviceInfo){
                    return error('已有服务绑定此分类,请勿修改上级分类');
                }
            }
        }


        $tabClassModel->where(['id' => $param['id']])->update($data);
        return success('修改成功');
    }
    public function updateStatus(Request $request){
        $param = $request->param();
        $tabClassModel = new \app\common\model\TabClass();
        $tabClassModel->where(['id' => $param['id']])->update(['status' => $param['status']]);
        return success('修改成功');
    }
    public function  addLabel(Request $request){
        $param = $request->param();
        if (empty( $param['name'])){
            return error('请输入名称');
        }
        $data = [
            'name' => $param['name'],
            'create_time' =>  time(),
            'pid' => $param['pid'],
            'type' => $param['type'],
            'sort' =>  $param['sort']
        ];

        $tabClassModel = new Label();
        $tabClassModel->insert($data);
        return success('添加成功');
    }
    public function labelList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where = [];
        if (isset($param['name']) && !empty($param['name'])){
            $where['name'] = $param['name'];
        }
        $labelModel = new Label();
        $tabList = $labelModel->where($where)/*->limit($start,$limit)*/->select()->toArray();
        $count = $labelModel->where($where)->count();
        return success('标签列表',['list' =>$tabList,'count' => $count]);
    }
    public function  editLabel(Request $request){
        $param = $request->param();
        $data = [
            'name' => $param['name'],
            'create_time' =>  time(),
            'pid' => $param['pid'],
            'type' => $param['type'],
            'sort' =>  $param['sort'],
        ];
        $tabClassModel = new \app\common\model\TabClass();
        $tabClassModel->where(['id' => $param['id']])->update($data);
        return success('修改成功');
    }

    public function  updateLabel(Request $request){
        $param = $request->param();
        $tabClassModel = new Label();;
        $tabClassModel->where(['id' => $param['id']])->update(['status' => $param]);
        return success('操作成功');
    }


}
